package com.ruoyi.tzzl.api;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * 实名认证api接口
 * @author by caoyong
 * @date 2022/12/5
 **/
@Slf4j
public class RealNameApi {

    /**实名认证*/
    public static String authentication(String mobile, String name) {
        String host = "http://yyseys.qianshutong.com";
        String path = "/web/interface/yyseysyz";
        String method = "POST";
        String appcode = "530d6a29bd2b472e8b47a79a2faa690a";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("mobile",mobile); //手机号码
        bodys.put("name", name); //姓名
        String result ="";
        try {
            /**
             * 重要提示如下:
             * HttpUtils请从
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
             * 下载
             *
             * 相应的依赖请参照
             * https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
             */
            HttpResponse response = HttpUtil.doPost(host, path, method, headers, querys, bodys);
            System.out.println(response.toString());
            //获取response的body
            String entity = EntityUtils.toString(response.getEntity());
            JSONObject jsonObject =JSONObject.parseObject(entity);
            log.info("用户实名认证:{},{}",name,jsonObject);
            JSONObject jsonObject1 = jsonObject.getJSONObject("data");
            JSONObject jsonObject2 = jsonObject1.getJSONObject("data");
            result = jsonObject2.getString("result");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


}
